<?php

class Product_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
		
		$this->load->module_model('admin', 's_category_model', 's_category');
		
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function get_posts($lang_id=null)
	{
		$this->db->select('*');
		/*$this->db->where('type','n');*/
		if($lang_id)
		{
			$this->db->where('lang_id',$lang_id);
		}
		$this->db->order_by('id', 'DESC');
			
		$query = $this->db->get($this->_table['products'] . ' products');
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			foreach (array_keys($result) as $key)
			{
				$category = $this->s_category->get_category($result[$key]['category_id']);//is not use at the moment/$this->category->get_categories_by_ids($this->get_post_categories($result[$key]['id']));
				//var_dump($category['title']);exit();
				$result[$key]['categories'] = $category['title'];
			}

			return $result;
		}
	}

	public function get_post($id)
	{
		$this->db->select('*');
		$this->db->where('id', $id);
			
		$query = $this->db->get($this->_table['products'], 1);
			
		if ($query->num_rows() == 1)
		{
			return $query->row_array();
		}
	}
	
	public function get_post_categories($news_id)
	{
		$this->db->select('category_id');
		$this->db->where('news_id', $news_id);
		
		$query = $this->db->get($this->_table['newscategory']);
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			
			foreach ($result as $category)
			{
				$categories[] = $category['category_id'];
			}
			
			return $categories;
		}
	}

	public function get_categories($lang_id=null)
	{
		$this->db->select('*');
		if($lang_id)
		{
			$this->db->where('lang_id',$lang_id);
		}
		/*$this->db->where('level',1);*/
		$query = $this->db->get($this->_table['s_category']);
			
		if ($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row)
			{
				$data[$row['id']] = $row;
			}

			return $data;
		}
	}
	
	public function get_tags_by_post_id($post_id)
	{
		$this->db->select('tags.id, tags.name');
		$this->db->from($this->_table['tags_to_posts'] . ' tags_to_posts');
		$this->db->join($this->_table['tags'] . ' tags', 'tags_to_posts.tag_id = tags.id', 'left');
		$this->db->where('post_id', $post_id);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();

			foreach ($query->result_array() as $row)
			{
				$data[$row['id']] = $row['name'];
			}
			
			$tags = implode(', ', $data);
			
			return $tags;
		}
	}
	
	public function get_tag_id($tag_name)
	{
		$this->db->select('id');
		$this->db->where('name', $tag_name);
		
		$query = $this->db->get($this->_table['tags'], 1);
		
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['id'];
		}
		else
		{
			return FALSE;
		}
	}
	
	public function parse_tags($tags)
	{
		$tags = str_replace(' ', '', $tags);
		$tags = explode(',', $tags);
		
		return $tags;
	}

	public function create_post($lang_id = null,$image=null)
	{
		
		$feature = ($this->input->post('feature') == 1) ? '1' : '0';
			
		$data = array
					(
						'views' => 0,
						'order'=> $this->get_last_order() + 1,						
						'lang_id'=>($lang_id)?$lang_id:2,
						'title' => $this->input->post('title'),
						'short_description' => html_entity_decode($this->input->post('fck_short_description'), ENT_NOQUOTES, 'UTF-8'),
						'description' => html_entity_decode($this->input->post('fck_description'), ENT_NOQUOTES, 'UTF-8'),
						'feature' => $feature,
						'price' => $this->input->post('price'),
						'code'=>$this->input->post('code'),
						'category_id'=>$this->input->post('categories'),
						'image'=>($image) ? $image['file_name']:'',
						'image_thumb'=>($image) ? $image['image_thumb']:''
					);
			
		$this->db->insert($this->_table['products'], $data);
	}
	
	public function add_newscategory($news_id, $category_id)
	{
		$data = array
					(
						'news_id' => $news_id,
						'category_id' => $category_id
					);
					
		$this->db->insert($this->_table['newscategory'], $data);
	}
	
	public function add_tag_to_post($tag_id, $post_id)
	{
		$data = array
					(
						'tag_id' => $tag_id,
						'post_id' => $post_id
					);
					
		$this->db->insert($this->_table['tags_to_posts'], $data);
	}

	public function add_tags($tags, $post_id)
	{
		foreach ($tags as $tag)
		{
			$tag_id = $this->get_tag_id($tag);
			
			if ($tag_id == FALSE) 
			{
				$data = array
							(
								'name' => $tag
							);
					
				$this->db->insert($this->_table['tags'], $data);
				
				$tag_id = $this->db->insert_id();
			}

			$this->add_tag_to_post($tag_id, $post_id);
		}
	}
	
	public function edit_post($id,$image=null)
	{
		$feature = ($this->input->post('feature') == 1) ? '1' : '0';
			
		$data = array
					(
						'views' => 0,
						'order'=> $this->get_last_order() + 1,						
						'lang_id'=>($lang_id)?$lang_id:2,
						'title' => $this->input->post('title'),
						'short_description' => html_entity_decode($this->input->post('fck_short_description'), ENT_NOQUOTES, 'UTF-8'),
						'description' => html_entity_decode($this->input->post('fck_description'), ENT_NOQUOTES, 'UTF-8'),
						'feature' => $feature,
						'price' => $this->input->post('price'),
						'code'=>$this->input->post('code'),
						'category_id'=>$this->input->post('categories'),
						'image'=>($image) ? $image['file_name']:$this->input->post('oldimage'),
						'image_thumb'=>($image) ? $image['image_thumb']:$this->input->post('oldimage_thumb')
					);
				
		$this->db->where('id', $this->input->post('id'));
		$this->db->update($this->_table['products'], $data);
	}
	public function edit_post_categories($news_id, $categories)
	{
		$this->db->select('id');
		$this->db->where('news_id', $news_id);
		$this->db->where_in('category_id', $categories);
			
		$query = $this->db->get($this->_table['newscategory']);

		if ($query->num_rows() != count($categories))
		{
			// delete old categories
			$this->delete_newscategory($news_id);
			
			// add new categories
			foreach ($categories as $category_id)
			{
				$this->add_newscategory($news_id, $category_id);
			}
		}
	}
	
	public function edit_post_tags($post_id, $tags)
	{
		// delete old tags
		$this->delete_post_tags($post_id);
		
		// add new tags (if any)
		if ($tags)
		{
			$this->add_tags($this->posts->parse_tags($tags), $post_id);
		}
		
		$this->check_for_orphaned_tags();
	}

	public function delete_news($news_id)
	{
		$this->db->where('id', $news_id);
		$this->db->delete($this->_table['news']);
		
		//$this->db->where('news_id', $news_id);
		//$this->db->delete($this->_table['tags_to_posts']);
		
		//$this->check_for_orphanned_comments();
		///$this->check_for_orphaned_tags();
	}
	
	public function delete_newscategory($news_id)
	{
		$this->db->where('news_id', $news_id);
		$this->db->delete($this->_table['newscategory']);
	}
	
	public function delete_post_tags($post_id)
	{
		$this->db->where('post_id', $post_id);
		$this->db->delete($this->_table['tags_to_posts']);
	}
	
	// Private or protected methods
	protected function check_for_orphanned_comments()
	{
		$this->db->select('comments.id');
		$this->db->from($this->_table['comments'] . ' comments');
		$this->db->join($this->_table['posts'] . ' posts', 'comments.post_id = posts.id', 'left');
		$this->db->where($this->_table['posts'] . '.id IS NULL');
		
		$query = $this->db->get();
		
		// we found orphaned comments, lets delete them
		if ($query->num_rows() > 0)
		{
			$results = $query->result_array();
			
			foreach ($results as $result)
			{
				$this->db->where('id', $result['id']);
				$this->db->delete($this->_table['comments']);
			}
		}
	}
	
	protected function check_for_orphaned_tags()
	{
		$this->db->select('tags.id');
		$this->db->from($this->_table['tags'] . ' tags');
		$this->db->join($this->_table['tags_to_posts'] . ' tags_to_posts', 'tags.id = tags_to_posts.tag_id', 'left');
		$this->db->where('tags_to_posts.tag_id IS NULL');
		
		$query = $this->db->get();
		
		// we found orphaned tags, lets delete them
		if ($query->num_rows() > 0)
		{
			$results = $query->result_array();
			
			foreach ($results as $result)
			{
				$this->db->where('id', $result['id']);
				$this->db->delete($this->_table['tags']);
			}
		}
	}

	protected function get_last_order()
	{
		$this->db->select('MAX(news.order) AS pos');
		
		$query = $this->db->get($this->_table['news'], 1);
			
		if ($query->num_rows() == 1)
		{
			$result = $query->row_array();
			
			return $result['pos'];
		}
	}
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */